package com.geekai.chat.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import com.geekai.chat.config.AiConfig;

@Service
public class AiModelFactory {

    private final AiConfig aiConfig;
    private final WebClient.Builder webClientBuilder;
    private final Map<String, AiConfig.ModelConfig> modelConfigs = new ConcurrentHashMap<>();

    public AiModelFactory(AiConfig aiConfig, WebClient.Builder webClientBuilder) {
        this.aiConfig = aiConfig;
        this.webClientBuilder = webClientBuilder;
    }

    public AiConfig.ModelConfig getModelConfig(String modelType) {
        return modelConfigs.computeIfAbsent(modelType, this::loadModelConfig);
    }

    private AiConfig.ModelConfig loadModelConfig(String modelType) {
        AiConfig.ModelConfig config = aiConfig.getModelConfig(modelType);
        if (config == null) {
            throw new IllegalArgumentException("Unsupported model type: " + modelType);
        }
        return config;
    }

    public WebClient.Builder getWebClientBuilder() {
        return webClientBuilder;
    }
}
